import os
import sys
from apps.goods.models import SKU
from utils.goods import get_categories,get_breadcrumb,get_goods_specs
from django.template import loader
from meiduo_mall.settings import BASE_DIR



def generic_detail_html(sku):
    categories = get_categories()
    breadcrumb = get_breadcrumb(sku.category)
    goods_specs = get_goods_specs(sku)

    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': goods_specs
    }
    detail_template=loader.get_template('detail.html')
    detail_html=detail_template.render(context)
    file_path=os.path.join(BASE_DIR,'front_end_pc/goods/%s.html'%sku.id)
    with open(file_path,'w',encoding='utf-8') as f:
        f.write(detail_html)


sys.path.insert(0,'../')
os.environ.setdefault("DJANGO_SETTINGS_MOULD","meiduo_mall.settings")
import django
django.setup()

skus=SKU.objects.all()
for sku in skus:
    generic_detail_html(sku)